package sample.templates;

import java.lang.reflect.Constructor;
import java.util.Enumeration;
import java.util.Vector;

public abstract class Trash2 {
	private double weight;

	Trash2() {

	}

	Trash2(double wt) {
		weight = wt;
	}

	abstract double value();

	double weight() {
		return weight;
	}

	public static void sumValue(Vector bin) {
		Enumeration e = bin.elements();
		double val = 0.0f;
		while (e.hasMoreElements()) {
			Trash t = (Trash) e.nextElement();

			val += t.weight() * t.value();
			System.out.println("weight of" + t.getClass().getName() + "="
					+ t.weight());

		}
		System.out.println("Total value=" + val);
	}

	// inner class
	public static class PrototypeNotFoundException extends Exception {
	}

	public static class CannotCreateTrashException extends Exception {
	}

	private static Vector trashType = new Vector();

	public static Trash factory(Info info) throws Exception, Exception {
		for (int i = 0; i < trashType.size(); i++) {
			Class tc = (Class) trashType.elementAt(i);
			if (tc.getName().indexOf(info.id) != -1) {

				Constructor ctor = tc.getConstructor(new Class[] { double.class });
				return (Trash) ctor.newInstance(new Object[] { new Double(info.data) });
			}
		}
		return factory(info);
	}


	public static class Info {

		public double data;
		public String id;

		public Info(String type, double weight) {
			this.id=type;
			this.data=weight;
		}
		

	}
}
